package com.example.syyao.myapp.Pot;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.syyao.myapp.Adapter.NVAdapter;
import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Response.Get_response;
import com.example.syyao.myapp.Response.Get_response_List;
import com.example.syyao.myapp.Response.Image;

import java.util.*;

import com.example.syyao.myapp.WebContact.JsonUtil;
import com.example.syyao.myapp.WebContact.WebAccessTools;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class Pot_content extends SwipeBackActivity {
    private ListView listView;
    private List<List<Image>> imagesList;
    private LinearLayout btn_back1;
    private List<Map<String, Object>> myinformation;
    private ConstraintLayout constraintLayout;
    private Get_response_List pot_list;
    NVAdapter nvAdapter;
    Get_response_List Movement_response;

    //列表
    @RequiresApi(api = Build.VERSION_CODES.ECLAIR)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pot_content);

        Intent intent = getIntent();
        String kind=intent.getStringExtra("kind");
        constraintLayout= (ConstraintLayout) findViewById(R.id.pot_layout);
        overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        LinearLayout btn_back1=(LinearLayout) findViewById(R.id.lay_back1);
        btn_back1.setOnClickListener(new Btnback1());



        listView= (ListView) findViewById(R.id.lv_main);
        initData(kind);


        Map<String, Object> map = new HashMap<>();
        map.put("head_picture", "1");
        map.put("nickname", "1");
        map.put("time", "1");
        map.put("tagname", "1");
        myinformation = new ArrayList<>();
        myinformation.add(map);

        imagesList = new ArrayList<>();
        List<Image> list = new ArrayList<>();
        list.add(new Image("http://120.79.205.71/picture/pic1.jpg", 250, 250));
        imagesList.add(list);

        nvAdapter = new NVAdapter(Pot_content.this, imagesList, myinformation);


        listView.setAdapter(nvAdapter);
    }


    protected void onStart() {

        super.onStart();




    }

    @RequiresApi(api = Build.VERSION_CODES.ECLAIR)
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0,R.anim.slide_out_right);
    }

    private class Btnback1 implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            finish();
        }
    }


    private void initData(final String kind) {
        new Thread(new Runnable() {

            @Override
            public void run() {
//                System.out.println(username);
                String webContent = WebAccessTools.Get_pot_content(kind);
                System.out.println("MOVEMENT:" + webContent);
                Movement_response = (Get_response_List) JsonUtil.jsonToObject(webContent,
                        Get_response_List.class);
                hand.sendEmptyMessage(0x123);
            }
        }).start();
    }


    @SuppressLint("HandlerLeak")
    private Handler hand = new Handler() {
        public void handleMessage(android.os.Message msg) {
            imagesList = new ArrayList<>();
            myinformation = new ArrayList<>();
            if (Movement_response != null && Movement_response.getMessage().equals("成功")) {
                List<Map<String, Object>> temp = Movement_response.getData();
                myinformation = new ArrayList<>();
                for (int i = 0; i < temp.size(); i++) {
                    //     String str = temp.get(i).get("tagname").toString().substring(1,temp.get(0).get("tagname").toString().length()-1);
                    String picture = temp.get(i).get("picturelist").toString().substring(1, temp.get(i).get("picturelist").toString().length() - 1);
                    picture = picture.replace(" ", "");
                    String[] arr = picture.split(",");
                    List<String> itemlist = Arrays.asList(arr);
                    ArrayList<Image> singleList = new ArrayList<>();
                    for (int j = 0; j < itemlist.size(); j++) {
                        singleList.add(new Image("http://120.79.205.71/picture/" + itemlist.get(j), 500, 500));
                    }
                    System.out.println("singls:" + singleList.size());
                    Map<String, Object> map = new HashMap<>();
                    map.put("nickname", Movement_response.getData().get(i).get("nickname").toString());
                    map.put("time", Movement_response.getData().get(i).get("time").toString());
                    map.put("head_picture", Movement_response.getData().get(i).get("head_picture").toString());
                    map.put("tagname", Movement_response.getData().get(i).get("taglist").toString());
                    myinformation.add(map);
                    imagesList.add(singleList);

                }
                nvAdapter.update(Pot_content.this,imagesList, myinformation);


            }
        }

        ;
    };
}
